<?php

require_once('system/core/Controller.php');

/**
 * @author Garry
 * ContentLineManagers handle multiple content areas
 */
class ContentLineManager extends CI_Controller {
	public $display;
	
	const ARTICLE = 1;
	
	const GALLERY = 2;
	//TODO - Insert your code here
	/**
	 *
	 */
	public  function __construct( $id ) {
		parent::__construct();
		$this->load->database();
		$this->display = new Display(); 
		parent::__construct();
		if( array_key_exists('editing', $_SESSION) ){
			$this->prep_edit_content($id);
		}
		else {
			$this->prep_content($id);
		}
	}
	
	/**
	 * 
	 * 
	 */
	public static function GetContentDao($id){
		$contentsection = ContentDao::Search( 'SectionDao', array( 'content_id' => $id ) );
		if( EMPTY( $contentsection ) ){
			$contentsection = new SectionDao(TRUE);
			$contentsection->content_id = $id;
		}
		return $contentsection;
	}
	/**
	 * Provides the editing pane for all editable content
	 */
	protected function prep_edit_content($id){
		$contentsection = self::GetContentDao( $id );
		$popup = new Display();
		$popup->id = $id;
		$popup->options = $options = array(
                  self::ARTICLE  => 'Article',
                  self::GALLERY => 'Gallery',
                ); 
		$popup->base = 'new_content_popup';
		$popup->action = 'edit/createContent';
		$popup->finalize();
		$this->display->addChild($popup,'popup');
		$childobjects = $contentsection->getObjects();
		$childoutputs = Array();
		foreach($childobjects as $child ){
			$childoutputs[]=$child->GetEditableDisplay();
		}
		$this->display->content_sections = $childoutputs; 
		$this->display->base = 'contentmanager';
	}
	
	/**
	 * Provides the display pane for all editable content
	 */
	protected function prep_content($id){
		$contentsection = self::GetContentDao( $id );
		$childobjects = $contentsection->getObjects();
		$childoutputs = Array();
		foreach($childobjects as $child ){
			$childoutputs[]=$child->GetDisplay();
		}
		$this->display->content_sections = $childoutputs; 
		$this->display->base = 'contentmanager_display';
	}
	
	/**
	 * Static methods
	 */
	public static function GetController( $which ){
		switch( $which ){
			case self::ARTICLE:
			return new ArticleContentController();
			case self::GALLERY:
			return new GalleryContentController();
			break;
			default:
			return new ArticleContentController();
		}
	}
	
	/**
	 * 
	 * Get the 
	 * @param unknown_type $which
	 * @param unknown_type $save
	 */
	public static function GetEditor( $which,$save = FALSE  ){
		switch( $which ){
			case self::ARTICLE:
			return new ArticleDao( $save );
			break;
			case self::GALLERY:
			return new GalleryDao( $save );
			break;
			default:
			return new ArticleDao( $save );
		}
	}
	
	/**
	 * Static methods
	 */
	public static function LoadEditor( $which,$id  ){
		switch( $which ){
			case self::ARTICLE:
			return new ArticleDao( $id );
			break;
			case self::GALLERY:
			return new GalleryDao( $save );
			default:
			return new ArticleDao( $id );
		}
	}
}


?>